package com.gdit.zax.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.BannerPicDao;
import com.gdit.zax.entitys.BaseEntity;
import com.gdit.zax.entitys.StartPic;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.O;

public class Banner extends HttpServlet {
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
		
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Connection conn = DBUtil.getConnection();
		if (conn==null) {
			O.printDBerror(response);
			return;
		}
		
		DAOFactory dao  = DAOFactory.init(conn, request, response);
		BannerPicDao bdao = dao.getBannerPicDao();
		
		
		List<StartPic> list = bdao.getBannerPic(1, 5);
		if (list.size()==0) {
			O.printFail(response, 1, "暂无首页图片数据!");
			return;
		}
		
		List<Map<String, Object>> newlist = new ArrayList<Map<String,Object>>();
		for (StartPic be:list) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", be.getId());
			map.put("type", be.getType());
			map.put("path", Constant.WEBRoot + be.getPath());
			map.put("data", be.getData());
			map.put("title", be.getTitle());
			newlist.add(map); 
		}
		O.printSuccess(response, newlist);
	}

}
